<?php
/*
KAP 0.3 : navmanager : xml

18/10/2004 11:13:47
*/

class waknavmanager extends wakCoreComponent {
/*----------------------------------------------------------------------------------------------
                                                                                      proprietes
----------------------------------------------------------------------------------------------*/
	var $default_parameters=array(
		'path'=>'data/',
		'file'=>'arbo.xml',
		'prefixUri'=>''
	);
/*----------------------------------------------------------------------------------------------
                                                                                    constructeur
----------------------------------------------------------------------------------------------*/
	function waknavManager(& $controller) {
		// appel du constructeur parent
		$const=get_parent_class($this);
		parent::$const($controller);
		// chargement de l'arbo
		//$this->_loadArbo();
	}
/*----------------------------------------------------------------------------------------------
                                                                              methodes publiques
----------------------------------------------------------------------------------------------*/
	function loadArbo($file, $prefixUri='') {
		$this->parameters['prefixUri']=$prefixUri;
		$this->parameters['file']=$file;
		$this->_loadArbo();
	}

	function getHome() {
		$label=$this->arbo->getContent("/arbo/label");
		//$title=$this->arbo->getContent("/arbo/title");
		return array('uri'=>'','fullUri'=>$this->parameters['prefixUri'],'label'=>$label);
	}

	function getTopLevel() {
		$list=($this->_getXpathChildren("/arbo"));
		$list=$this->_addFullUri($list);
		return $list;
	}
	
	function getTree($xpath="/arbo") {
		$label=$this->arbo->getcontent($xpath."/label");
		$fullUri=$this->_getXpathFullUri($xpath);
		$children=array();
		$n=$this->arbo->count($xpath."/page");
		for ($f=1; $f<=$n; $f++)
			$children[]=$this->getTree($xpath."/page[$f]");
		return array('label'=>$label,'fullUri'=>$fullUri,'children'=>$children);
	}
	
	function getParent($path=NULL) {
		if ($path==NULL) $path=CALL_URI;
		if ($path=="/") return false;
		// xpath courant
		$xpath=$this->_translatePathToXpath($path); //echo $xpath;
		// on descend d'un niveau xpath jusqu'a trouver la racine
		$xpath=substr($xpath,0,strrpos($xpath,"/"));
		$link=$this->_getXpathLink($xpath);
		return $link;
	}
	
	function getPathFromRoot($path=NULL) {
		if ($path==NULL) $path=CALL_URI;
		if ($this->parameters['prefixUri']) {
			$path=str_replace('/'.$this->parameters['prefixUri'],'',$path);
		}
		// xpath courant
		$xpath=$this->_translatePathToXpath($path);
		$list=array();
		while ($link=$this->_getXpathLink($xpath)) {
			$list[]=$link;
			// on descend d'un cran
			$xpath=substr($xpath,0,strrpos($xpath,'/'));
		}
		// accueil
		$list[]=$this->getHome();
		$list=array_reverse($list);
		//echo affvar($list);
		return $list;
	}

	function getChildren($path=NULL) {
		if ($path==NULL) $path=CALL_URI;
		if ($this->parameters['prefixUri']) {
			$path=str_replace('/'.$this->parameters['prefixUri'],'',$path);
		}
		$xpath=$this->_translatePathToXpath($path);
		$list=$this->_getXpathChildren($xpath);
		//$list=$this->_addFullUri($list,$path);
		return $list;
	}
	
	function getSameLevel($path=NULL) {
		if ($path==NULL) $path=CALL_URI;
		if ($this->parameters['prefixUri']) {
			$path=str_replace('/'.$this->parameters['prefixUri'],'',$path);
		}
		// xpath courant
		$xpath=$this->_translatePathToXpath($path); //echo $xpath;
		// on descend d'un niveau xpath
		$xpath=substr($xpath,0,strrpos($xpath,"/"));
		$list=$this->_getXpathChildren($xpath);

		// on descend d'un niveau le path de reference
		//$basepath=substr($path,0,strrpos($path,"/"));
		//$list=$this->_addFullUri($list,$basepath);
		
		// on enleve la page en cours
		$newlist=array();
		foreach ($list as $item) {
			//echo $item['fullUri']." : ".$path."<br/>";
			if ("/".$item['fullUri']!=$path) $newlist[]=$item;
		}
		return $newlist;
	}
	
	function upOneLevel($path=NULL) {
		$parent=$this->getparent($path);
		return $parent['fullUri'];
	}
	
	function getSelfFullUri($path=NULL) {
	   if ($path==NULL) $path=CALL_URI;
		// xpath courant
		$xpath=$this->_translatePathToXpath($path);
		return $this->_getXpathFullUri($xpath);
	}
/*----------------------------------------------------------------------------------------------------
																												methodes privees
----------------------------------------------------------------------------------------------------*/
	function _loadArbo() {
		$file=$this->parameters['path'].$this->parameters['file'];
		$this->arbo=$this->C->needObject('xmldoc',$file);
		//echo htmlentities($this->arbo->getall());
		//echo affvar($this);
	}
	
	function _translatePathToXpath($path="") {
		if (substr($path,0,1)=="/") $path=substr($path,1); // enlevement slash initial
		// xpath de base
		return $this->_getFullUriXpath($path);
	}
	
	function _getXpathChildren($xpath) {
		$xpath.="/page";
		$n=$this->arbo->count($xpath);
		$list=array();
		for ($f=1; $f<=$n; $f++) {
			$uri=$this->arbo->getContent($xpath."[$f]/@uri");
			$label=$this->arbo->getContent($xpath."[$f]/label");
			$fulluri=$this->_getXpathFulluri($xpath."[$f]");
			//if ($this->parameters['prefixUri']) $fulluri=$this->parameters['prefixUri'].'/'.$fulluri;
			$list[]=array(
				'uri' => $uri,
				'label' => $label,
				'fullUri'=>$fulluri);
		}
		return $list;
	}
	
	function _getXpathLink($xpath) {
		if (!$uri=$this->arbo->getContent($xpath."/@uri")) return false;
		$label=$this->arbo->getContent($xpath."/label");
		
		// recherche fulluri
		$fulluri=$this->_getXpathFullUri($xpath);
		/*$steps=array();
		while ($stepUri=$this->arbo->getContent($xpath."/@uri")) {
			$steps[]=$stepUri;
			// on descend d'un cran
			$xpath=substr($xpath,0,strrpos($xpath,'/'));
		}
		$steps=array_reverse($steps); $fulluri=join('/',$steps);*/

		return array('uri'=>$uri,'label'=>$label,'fullUri'=>$fulluri);
	}

	function _addFullUri($list, $base="/") {
		if (substr($base,0,1)=="/") $base=substr($base,1); //enlevement slash initial
		$base=$this->parameters['prefixUri'].$base;
		if ($base) $base=$base."/"; // ajout slash final sauf racine
		for (reset($list); $item= current($list); next($list)) {
			$list[key($list)]['fullUri']=$base.$item['uri'];
		}
		return $list;
	}
	
	// recherche le xpath correspondant Ó l'uri, retourne le premier xpath valide depuis la racine
	function _getFulluriXpath($fulluri="") {
		if (!$fulluri) return false;
		// recup uri
		$steps=explode('/',$fulluri);
		$xpath="/arbo";
		foreach ($steps as $step) {
			if ($this->arbo->get($xpath."/page[@uri='$step']")) $xpath.="/page[@uri='$step']";
			else break;
		}
		return $xpath;
	}
	
	function _getXpathFulluri($xpath="/arbo") {
		// on remonte vers la racine
		$steps=array();
		while ($xpath and $xpath!="/arbo") {
			//echo $xpath."::";
			$steps[]=$this->arbo->getContent($xpath."/@uri");
			// on descend d'un cran
			$xpath=substr($xpath,0,strrpos($xpath,"/"));
		}
		$steps=array_reverse($steps);
		$fulluri=join('/',$steps);
		if ($this->parameters['prefixUri']) $fulluri=$this->parameters['prefixUri'].'/'.$fulluri;
		return $fulluri;
	}
}
?>
